
from rest_framework.generics import ListAPIView, RetrieveAPIView

from apps.users.models import User
from datetime import date
from rest_framework.views import APIView
from rest_framework.response import Response


class DailyActiveView(APIView):

    def get(self, request):
        # 1.获取当天的日期
        today = date.today()
        # 2.过滤查询
        # today  是一个日期              年月日
        # last_login 是一个日期时间       年月日 时分秒

        # last_login__gte=today
        # django 会把today 变为 年月日 00:00:00
        count = User.objects.filter(last_login__gte=today).count()
        # count=User.objects.filter(last_login=today).count()
        # 3.返回数量
        return Response({'count': count})



class DailyOrderCountAPIView(APIView):

    def get(self, request):
        today = date.today()

        count = User.objects.filter(orderinfo__create_time__gte=today).count()

        return Response({'count': count})


from datetime import timedelta


class MonthAPIView(APIView):

    def get(self, request):
        # 1.确定数据形式
        data = []
        # 2.我们需要确定从哪天到哪天
        today = date.today()
        # 30天前的日期
        start_date = today - timedelta(days=30)

        # 3.遍历获取某一天的用户增量
        for i in range(30):
            # 那一天
            index = start_date + timedelta(days=i)
            # 后一天
            after = start_date + timedelta(days=i + 1)

            # count=User.objects.filter(date_joined__gte=那一天, date_joined__lt=后一天)
            count = User.objects.filter(date_joined__gte=index, date_joined__lt=after).count()

            # 追加
            data.append({
                'count': count,
                'date': index
            })

        return Response(data)



